package com.siwei.mes.service.experiment.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.experiment.ExperimentOuter;
import com.siwei.mes.mapper.experiment.ExperimentOuterMapper;
import com.siwei.mes.mapper.materials.MaterialsSpecConfigMapper;
import com.siwei.mes.service.experiment.ExperimentOuterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 外委托(ExperimentOuter)表服务实现类
 *
 * @author linzi
 * @since 2024-07-26 21:00:44
 */
@Service
public class ExperimentOuterServiceImpl implements ExperimentOuterService {
    @Resource
    private ExperimentOuterMapper experimentOuterMapper;
    @Resource
    private MaterialsSpecConfigMapper materialsSpecConfigMapper;

    @Override
    public Long create(ExperimentOuter entity) {
        entity.init(true);
        assertTrue(materialsSpecConfigMapper.selectByPrimaryKey(entity.getSpecId()) == null, "材料及规格配置信息不存在");
        experimentOuterMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(ExperimentOuter entity) {
        ExperimentOuter experimentOuter = experimentOuterMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentOuter == null, "外委托信息不存在");

        assertTrue(materialsSpecConfigMapper.selectByPrimaryKey(entity.getSpecId()) == null, "材料及规格配置信息不存在");
        entity.init(false);
        experimentOuterMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentOuter experimentOuter = experimentOuterMapper.selectByPrimaryKey(id);
        assertTrue(experimentOuter == null, "外委托信息不存在");
        experimentOuterMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentOuter> getPage(PageQuery<ExperimentOuter, ExperimentOuter> query) {
        ExperimentOuter searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentOuterMapper.getList(searchParam));
    }

    @Override
    public ExperimentOuter loadById(Long id) {
        return experimentOuterMapper.loadById(id);
    }

    @Override
    public List<ExperimentOuter> getAll(ExperimentOuter entity) {
        return experimentOuterMapper.getList(entity);
    }
}
